package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class HelperWithWebDriverBase extends HelperBase {
	
	private WebDriver driver;

	public HelperWithWebDriverBase(ApplicationManager manager) {
		super(manager);
		driver = manager.getWebDriverHelper().getDriver();
	}
		
	public boolean isElementPresent(By by) {
	    try {
	    driver.findElement(by);
	      return true;
	    } catch (NoSuchElementException e) {
	      return false;
	    }
	  }

	public boolean isAlertPresent() {
	    try {
	    driver.switchTo().alert();
	      return true;
	    } catch (NoAlertPresentException e) {
	      return false;
	    }
	  }

	protected WebElement findElement(By locator) {
		try {
			return driver.findElement(locator);
		} catch (Exception e){
			return null;
		}
	}
	
	protected List<WebElement> findElements(By locator) {
		return driver.findElements(locator);
	}

	protected void type(By locator, String text) {
		if (text != null) {
			driver.findElement(locator).clear();
			driver.findElement(locator).sendKeys(text);
		}
	}

	protected void click(By locator) {
		driver.findElement(locator).click();
	}


	protected void selectByText(By locator, String text) {
		if (text != null && !text.isEmpty()) {
			new Select(driver.findElement(locator)).selectByVisibleText(text);
		}
	}
	
	protected void openUrl(String string) {
		driver.get(manager.getProperty("baseUrl") + string);
	}
	
	protected void openAbsoluteUrl(String string) {
		driver.get(string);
	}
	
	protected String getTitle(){
		return driver.getTitle();
	}
}
